package com.example.oj_system.common;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 封装文件读写操作
 */
public class FileUtils {
    // 读出指定文件的内容, 以字符串形式返回
    public static String readFile(String filePath) {
        StringBuilder result = new StringBuilder();
        try(FileReader fileReader = new FileReader(filePath)) {
            while (true) {
                int ch = fileReader.read();
                if (ch == -1)
                    break;
                result.append((char)ch);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    // 将指定的字符串写入指定的文件中
    public static void writeFile(String filePath, String content) {
        try(FileWriter fileWriter = new FileWriter(filePath)) {
            fileWriter.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
